home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / dkbsrc.zip / PLANES.C < prev    next >
C/C++ Source or Header  |  1991-05-04  |  7KB  |  230 lines

  1. /*****************************************************************************
  2. *
  3. *                                    planes.c
  4. *
  5. *   from DKBTrace (c) 1990  David Buck
  6. *
  7. *  This module implements functions that manipulate planes.
  8. *
  9. * This software is freely distributable. The source and/or object code may be
  10. * copied or uploaded to communications services so long as this notice remains
  11. * at the top of each file.  If any changes are made to the program, you must
  12. * clearly indicate in the documentation and in the programs startup message
  13. * who it was who made the changes. The documentation should also describe what
  14. * those changes were. This software may not be included in whole or in
  15. * part into any commercial package without the express written consent of the
  16. * author.  It may, however, be included in other public domain or freely
  17. * distributed software so long as the proper credit for the software is given.
  18. *
  19. * This software is provided as is without any guarantees or warranty. Although
  20. * the author has attempted to find and correct any bugs in the software, he
  21. * is not responsible for any damage caused by the use of the software.  The
  22. * author is under no obligation to provide service, corrections, or upgrades
  23. * to this package.
  24. *
  25. * Despite all the legal stuff above, if you do find bugs, I would like to hear
  26. * about them.  Also, if you have any comments or questions, you may contact me
  27. * at the following address:
  28. *
  29. *     David Buck
  30. *     22C Sonnet Cres.
  31. *     Nepean Ontario
  32. *     Canada, K2H 8W7
  33. *
  34. *  I can also be reached on the following bulleton boards:
  35. *
  36. *     OMX              (613) 731-3419
  37. *     Mystic           (613) 596-4249  or  (613) 596-4772
  38. *
  39. *  Fidonet:   1:163/109.9
  40. *  Internet:  dbuck@ccs.carleton.ca
  41. *  The "You Can Call Me RAY" BBS    (708) 358-5611
  42. *
  43. *  IBM Port by Aaron A. Collins. Aaron may be reached on the following BBS'es:
  44. *
  45. *     The "You Can Call Me RAY" BBS (708) 358-5611
  46. *     The Information Exchange BBS  (708) 945-5575
  47. *
  48. *****************************************************************************/
  49.  
  50.  
  51. #include "frame.h"
  52. #include "vector.h"
  53. #include "dkbproto.h"
  54.  
  55. METHODS Plane_Methods =
  56.    { Object_Intersect, All_Plane_Intersections,
  57.      Inside_Plane, Plane_Normal,
  58.      Copy_Plane,
  59.      Translate_Plane, Rotate_Plane,
  60.      Scale_Plane, Invert_Plane};
  61.  
  62. extern PLANE *Get_Plane_Shape();
  63.  
  64. extern RAY *VP_Ray;
  65. extern long Ray_Plane_Tests, Ray_Plane_Tests_Succeeded;
  66.  
  67. int All_Plane_Intersections (Object, Ray, Depth_Queue)
  68.    OBJECT *Object;
  69.    RAY *Ray;
  70.    PRIOQ *Depth_Queue;
  71.    {
  72.    PLANE *Shape = (PLANE *) Object;
  73.    DBL Depth;
  74.    VECTOR Intersection_Point;
  75.    INTERSECTION Local_Element;
  76.  
  77.    if (Intersect_Plane (Ray, Shape, &Depth))
  78.       if (Depth > Small_Tolerance)
  79.          {
  80.          Local_Element.Depth = Depth;
  81.          Local_Element.Object = Shape -> Parent_Object;
  82.          VScale (Intersection_Point, Ray -> Direction, Depth);
  83.          VAdd (Intersection_Point, Intersection_Point, Ray -> Initial);
  84.          Local_Element.Point = Intersection_Point;
  85.          Local_Element.Shape = (SHAPE *)Shape;
  86.          pq_add (Depth_Queue, &Local_Element);
  87.          return (TRUE);
  88.          }
  89.  
  90.    return (FALSE);
  91.    }
  92.  
  93. int Intersect_Plane (Ray, Plane, Depth)
  94.    RAY *Ray;
  95.    PLANE *Plane;
  96.    DBL *Depth;
  97.    {
  98.    DBL NormalDotOrigin, NormalDotDirection;
  99.  
  100.    Ray_Plane_Tests++;
  101.    if (Ray == VP_Ray) {
  102.       if (!Plane->VPCached) {
  103.          VDot (Plane->VPNormDotOrigin, Plane->Normal_Vector, Ray->Initial);
  104.          Plane->VPNormDotOrigin += Plane->Distance;
  105.          Plane->VPNormDotOrigin *= -1.0;
  106.          Plane->VPCached = TRUE;
  107.          }
  108.  
  109.       VDot (NormalDotDirection, Plane->Normal_Vector, Ray->Direction);
  110.       if ((NormalDotDirection < Small_Tolerance) &&
  111.           (NormalDotDirection > -Small_Tolerance))
  112.          return (FALSE);
  113.  
  114.       *Depth = Plane->VPNormDotOrigin / NormalDotDirection;
  115.       if ((*Depth >= Small_Tolerance) && (*Depth <= Max_Distance)) {
  116.          Ray_Plane_Tests_Succeeded++;
  117.          return (TRUE);
  118.          }
  119.       else
  120.          return (FALSE);
  121.       }
  122.    else {
  123.       VDot (NormalDotOrigin, Plane->Normal_Vector, Ray->Initial);
  124.       NormalDotOrigin += Plane->Distance;
  125.       NormalDotOrigin *= -1.0;
  126.  
  127.       VDot (NormalDotDirection, Plane->Normal_Vector, Ray->Direction);
  128.       if ((NormalDotDirection < Small_Tolerance) &&
  129.           (NormalDotDirection > -Small_Tolerance))
  130.             return (FALSE);
  131.  
  132.       *Depth = NormalDotOrigin / NormalDotDirection;
  133.       if ((*Depth >= Small_Tolerance) && (*Depth <= Max_Distance)) {
  134.          Ray_Plane_Tests_Succeeded++;
  135.          return (TRUE);
  136.          }
  137.       else
  138.          return (FALSE);
  139.       }
  140.    }
  141.  
  142. int Inside_Plane (Test_Point, Object)
  143.    VECTOR *Test_Point;
  144.    OBJECT *Object;
  145.    {
  146.    PLANE *Plane = (PLANE *) Object;
  147.    DBL Temp;
  148.  
  149.    VDot (Temp, *Test_Point, Plane->Normal_Vector);
  150.    return ((Temp + Plane->Distance) <= Small_Tolerance);
  151.    }
  152.  
  153. void Plane_Normal (Result, Object, Intersection_Point)
  154.    OBJECT *Object;
  155.    VECTOR *Result, *Intersection_Point;
  156.    {
  157.    PLANE *Plane = (PLANE *) Object;
  158.  
  159.    *Result = Plane->Normal_Vector;
  160.    }
  161.  
  162. void *Copy_Plane (Object)
  163.    OBJECT *Object;
  164.    {
  165.    PLANE *New_Shape;
  166.  
  167.    New_Shape = Get_Plane_Shape ();
  168.    *New_Shape = * ((PLANE *)Object);
  169.    New_Shape -> Next_Object = NULL;
  170.  
  171.    if (New_Shape->Shape_Texture != NULL)
  172.       New_Shape->Shape_Texture = Copy_Texture (New_Shape->Shape_Texture);
  173.  
  174.    return (New_Shape);
  175.    }
  176.  
  177. void Translate_Plane (Object, Vector)
  178.    OBJECT *Object;
  179.    VECTOR *Vector;
  180.    {
  181.    PLANE *Plane = (PLANE *) Object;
  182.    VECTOR Translation;
  183.  
  184.    VEvaluate (Translation, Plane->Normal_Vector, *Vector);
  185.    Plane->Distance -= Translation.x + Translation.y + Translation.z;
  186.  
  187.    Translate_Texture (&Plane->Shape_Texture, Vector);
  188.    }
  189.  
  190. void Rotate_Plane (Object, Vector)
  191.    OBJECT *Object;
  192.    VECTOR *Vector;
  193.    {
  194.    TRANSFORMATION Transformation;
  195.  
  196.    Get_Rotation_Transformation (&Transformation, Vector);
  197.    MTransformVector (&((PLANE *) Object)->Normal_Vector,
  198.                      &((PLANE *) Object)->Normal_Vector, &Transformation);
  199.  
  200.    Rotate_Texture (&((PLANE *) Object)->Shape_Texture, Vector);
  201.    }
  202.  
  203. void Scale_Plane (Object, Vector)
  204.    OBJECT *Object;
  205.    VECTOR *Vector;
  206.    {
  207.    DBL Length;
  208.    PLANE *Plane = (PLANE  *) Object;
  209.  
  210.    Plane->Normal_Vector.x = Plane->Normal_Vector.x / Vector->x;
  211.    Plane->Normal_Vector.y = Plane->Normal_Vector.y / Vector->y;
  212.    Plane->Normal_Vector.z = Plane->Normal_Vector.z / Vector->z;
  213.  
  214.    VLength(Length, Plane->Normal_Vector);
  215.    VScale (Plane->Normal_Vector, Plane->Normal_Vector, 1.0 / Length);
  216.    Plane->Distance /= Length;
  217.  
  218.    Scale_Texture (&((PLANE *) Object)->Shape_Texture, Vector);
  219.    }
  220.  
  221. void Invert_Plane (Object)
  222.    OBJECT *Object;
  223.    {
  224.    PLANE *Plane = (PLANE  *) Object;
  225.  
  226.    VScale (Plane->Normal_Vector, Plane->Normal_Vector, -1.0);
  227.    Plane->Distance *= -1.0;
  228.    }
  229.  
  230.